package ru.rchervot.sanasar.client.event;

import ru.rchervot.sanasar.client.model.ItemObject;

import com.google.gwt.event.shared.GwtEvent;

public class EventResultsItem<T extends ItemObject> extends GwtEvent<HandlerResultsItem<T>> {
	private static Type<HandlerResultsItem<?>> TYPE = new Type<HandlerResultsItem<?>>();
	
	private T item;
	private Integer queryId;
	private Integer offset;
	
	public EventResultsItem() {}
	
	public EventResultsItem(T item, Integer queryId, Integer offset) {
		this.item = item;
		this.queryId = queryId;
		this.offset = offset;
	}
	
	@Override
	protected void dispatch(HandlerResultsItem<T> handler) {
		handler.onNewResultsItem(this);
	}

	/**
	 * Gets the type associated with this event.
	 * 
	 * @return returns the handler type
	 */
	public static Type<HandlerResultsItem<?>> getType() {
		if (TYPE == null) {
			TYPE = new Type<HandlerResultsItem<?>>();
		}
		return TYPE;
	}


	// The instance knows its PlaceChangeEventHandler is of type T, but the TYPE
	// field itself does not, so we have to do an unsafe cast here.
	@SuppressWarnings("unchecked")
	@Override
	public Type<HandlerResultsItem<T>> getAssociatedType() {
		return (Type) TYPE;
	}

	public T getItem() {
		return item;
	}
	
	public Integer getOffset() {
		return offset;
	}
	
	public Integer getQueryId() {
		return queryId;
	}
	
	public static <T extends ItemObject> 
	EventResultsItem<T> build(T item, Integer queryId, Integer offset) {
		return new EventResultsItem<T>(item, queryId, offset);
	}

}
